#-------------------------------------------------------------------------------
# Name:        Renderer
# Purpose:
#
# Author:      Pablo
#
# Created:     11/01/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os, logging
from ..tiledtmxloader import helperspygame

class Renderer():

  """handels rendering the world map to the screen"""

  def __init__(self):
    self.resources = helperspygame.ResourceLoaderPygame()
    self.renderer = helperspygame.RendererPygame()


  def load(self, tmxmap, size):

    """ Loads the specified map file"""

    # Boilderplate map loading stuff
    logging.debug('Loading map resources')
    self.resources.load(tmxmap)
    self.spritelayers = helperspygame.get_layers_from_map(self.resources)
    # Initiate the camera
    self.renderer.set_camera_position_and_size(0,0,size[0],size[1], "topleft")


    # Here the engine collapses the tiles into larger chunks, to improve performance.
    for i in range(len(self.spritelayers)):

      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])

      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])
      self.spritelayers[i] = helperspygame.SpriteLayer.collapse(self.spritelayers[i])

      # Loads paralax scrolling from the map file, and applies to to the layers
      paralax = [1,1]
      if 'paralax-x' in tmxmap.layers[i].properties:
        paralax[0] = float(tmxmap.layers[i].properties['paralax-x'])
      if 'paralax-y' in tmxmap.layers[i].properties:
        paralax[1] = float(tmxmap.layers[i].properties['paralax-y'])
      if not self.spritelayers[i].is_object_group:
        self.spritelayers[i].set_layer_paralax_factor(paralax[0], paralax[1])


  def showCollisions(self, tmxmap):

    """ Toggles drawing of the collisions layer """

    for i in range(len(tmxmap.layers)):
      if tmxmap.layers[i].name == 'Collisions':
        if self.spritelayers[i].visible == False:
          self.spritelayers[i].visible = True
        else:
          self.spritelayers[i].visible = False


  def moveCamera(self, pos, align='topleft'):

    """ Moves camera to speficied location. Optionally can define the alignment of the
    camera to that position. """

    self.renderer.set_camera_position(pos[0], pos[1], align)
##    self.camerapos = pos


  def render(self, screen):

    """ Draw the map to the specifed surface. """

    for spritelayer in self.spritelayers:
      if spritelayer.is_object_group:
        continue
      else:
        self.renderer.render_layer(screen, spritelayer)

    return screen


